package com.campus.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.shop.common.ConstantProperties;
import com.campus.shop.common.Errors;
import com.campus.shop.entity.User;
import com.campus.shop.exception.ServiceException;
import com.campus.shop.mapper.UserMapper;
import com.campus.shop.service.UserService;
import com.campus.shop.utils.ToolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @since 2023-12-25 16:32:04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean insertUser(User user) {
        return this.save(user);
    }

    @Override
    public Boolean updateUser(User user) {
        return this.updateById(user);
    }

    @Override
    public Boolean deleteUser(Long id) {
        return this.removeById(id);
    }

    @Override
    public User queryById(Long id) {
        return this.getById(id);
    }

    @Override
    public HashMap<String, Object> pageList(Integer currentPage) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Page<User> Userpage = new Page<>(currentPage, ConstantProperties.limit);
        Page<User> page = userMapper.selectPage(Userpage, queryWrapper);
        List<User> list = page.getRecords(); //当前页数数据
        long total = page.getTotal(); //总条数
        HashMap<String, Object> res = new HashMap<>();
        res.put("total", total);
        res.put("list", list);
        return res;
    }

    @Override
    public User userLogin(String userInput, String password) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.select("id,user_name,telephone,avatar,password,account_money");
        userQueryWrapper.eq("telephone", userInput).or().eq("user_name",userInput);
        userQueryWrapper.eq("status",ConstantProperties.normalStatus);
        User user = userMapper.selectOne(userQueryWrapper);
        if(Objects.isNull(user) || !user.getPassword().equals(ToolUtils.md5(password))){
            throw new ServiceException(Errors.USER_PASSWORD_ERROR);
        }
        return user;
    }

    @Override
    public Boolean userRegister(String telephone, String password) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("telephone", telephone);
        userQueryWrapper.eq("status",ConstantProperties.normalStatus);
        User user = userMapper.selectOne(userQueryWrapper);
        if(!Objects.isNull(user)){
            throw new ServiceException(Errors.PHONE_HAS_REGISTER);
        }
        User userInfo = new User();
        userInfo.setUserName(ToolUtils.getUserName());
        userInfo.setTelephone(telephone);
        userInfo.setPassword(ToolUtils.md5(password));
        int insert = userMapper.insert(userInfo);
        return insert == 1;
    }
}

